package edu.xidian.onlinedocument.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.xidian.onlinedocument.views.groups.*;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**********
 * 约束注解：
 * 1、@NotNull：Integer、Bigdecimal、LocalDateTime、LocalDate、实体类
 * 2、@NotBlank：String
 * 3、@NotEmpty：List
 * 4、@Size：字符串长度限制
 * 5、@Future：未来时间，LocalDateTime、LocalDate
 * 6、@Past：过去时间，LocalDateTime、LocalDate
 * 7、@DecimalMin：Bigdecimal最小值
 * 8、@DecimalMax：Bigdecimal最大值
 * 9、@Max：Integer最大值
 * 10、@Min：Integer最小值
 * 11、@Pattern：正则表达式校验
 */

@Entity
@Table(name = "user_data")
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UserData implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY) //AUTO表示主键由程序生成，IDENTITY表示主键由数据库生成（自增主键）
    @Column(name = "user_id", updatable = false, nullable = false)
    @NotNull(message = "用户id不能为空", groups = DataGroupD.class)
        private Integer userId;

    @Column(name = "user_name", nullable = false)
    @NotBlank(message = "用户昵称不能为空", groups = DataGroupA.class)
    @Size(min = 4, max = 20, message = "用户昵称的长度在4到20个字符之间", groups = DataGroupA.class)
        private String userName;

    @Column(name = "user_num", nullable = false, unique = true)
    @NotBlank(message = "用户账号不能为空", groups = DataGroupB.class)
    @Pattern(regexp = "^([a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+)|(1[0-9]\\d{9})$",
            message = "账号只能是邮箱或11位手机号", groups = DataGroupB.class)
        private String userNum;

    @Column(name = "user_key", nullable = false)
    @NotBlank(message = "用户密码不能为空", groups = DataGroupB.class)
    @Size(min = 6, max = 18, message = "密码太短或太长", groups = DataGroupC.class)
        private String userKey;

    @OneToMany(mappedBy = "creatorData")    // 注意，mappedBy的对象是对象类型中相对的@ManyToOne的变量名
    // @JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    @JsonIgnoreProperties({"creatorData", "documentEditor"})       // 防止连接后生成的json文件产生死循环
        private List<DocumentData> createDocumentData;

    @OneToOne
    @JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    @JsonIgnoreProperties("userData")       // 防止连接后生成的json文件产生死循环
    @JsonIgnore
        private UserTokenData userTokenData;

    @OneToMany(mappedBy = "editorData")
    // @JoinColumn(name="user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    @JsonIgnoreProperties({"editorData"})       // 防止连接后生成的json文件产生死循环，双向连接必须要
        private List<DocumentEditor> editDocumentData;

    @JsonProperty
    public void setUserId(int userId){
        this.userId = userId;
    }

    @JsonProperty
    public void setUserKey(String userKey){
        this.userKey = userKey;
    }


    @JsonIgnore
    public int getUserId(){
        return this.userId;
    }

    @JsonIgnore
    public String getUserKey(){
        return this.userKey;
    }

    /*
    public interface ViewName{};

    public interface ViewNameAndNum extends ViewName{};

    public interface ViewAllUserInfo extends ViewNameAndNum{};

    public interface ViewNameAndNumAndDoc extends ViewNameAndNum{};

    public interface ViewNameAndNumAndToken extends ViewNameAndNum{};

     */
    // 特别说明：带@Getter和@Setter或@Data注解的类在编译后会自动生成一系列的get和set方法，不用手动写
    // 特别说明：@Data除了包含@Getter和@Setter，还包含@ToString和@EqualsAndHashCode，一般用@Data比较好
    /*
    public int getUserId(){
        return userId;
    }

    public String getUserName(){
        return userName;
    }

    public String getUserNum(){
        return userNum;
    }

    public String getUserKey(){
        return userKey;
    }
     */
}

